<?php

namespace App\Http\Controllers\Innerend;

use App\Models\SiteConfig;
use App\Models\SiteWechat;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Crypt;
use App\Models\Admin;
use Illuminate\Support\Facades\Redirect;

class SpiderController extends BaseController
{
    // 开关

    // 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 公众号：爬虫开关
    // http://slh.com/innerend/spider/wechat
    // http://slh.shenlannews.com/innerend/spider/wechat
    public function wechat()
    {
        $method = $this->request->method();
        $wechDb = new SiteWechat();
        $where  = [['status', '>=', '0'],['fakeid', '<>', '']];
        // 保存
        if(strtolower($method) == 'post'){
            $lists = $wechDb->isCache(true)->getList($where, 0);
            $data  = $this->getData();
            $lists = array_column($lists, null, 'fakeid');
            foreach($data as $key => $val){
                $item = $lists[$key] ?? [];
                if($item && ($item['status'] != $val)){
                    $wechDb->where('fakeid', $key)->update(['status' => $val]);
                }
            }
            // 删除缓存
            $wechDb->deleteCacheList($lists);
        }
        $lists = $wechDb->isCache(true)->getList($where, 0);

        return $this->dispaly('spider/wechat', ['lists' => $lists, 'title' => '公众号抓取开关']);
    }

    // 直播开关
    // http://slh.com/innerend/spider/live
    // http://slh.shenlannews.com/innerend/spider/live
    public function live()
    {
        $method = $this->request->method();
        $confDb = new SiteConfig();
        $where  = [['field', '=', 'page_live']];
        // 保存
        if(strtolower($method) == 'post'){
            $lists = $confDb->isCache(true)->getList($where, 0);
            $data  = $this->getData();
            $lists = array_column($lists, null, 'field');
            foreach($data as $key => $val){
                $item = $lists[$key] ?? [];
                if($item && ($item['value'] != $val)){
                    $confDb->where('field', $key)->update(['value' => $val]);
                }
            }
            // 删除缓存
            $confDb->deleteCacheList($lists);
        }
        $lists = $confDb->isCache(true)->getList($where, 0);

        return $this->dispaly('spider/config', ['lists' => $lists, 'title' => '直播开关']);
    }

}


